package com.rgp.servicemap.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.common.UrlConstants;
import com.rgp.internalcommon.dto.AroundSearchDto;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.internalcommon.response.AroundSearchResponse;
import com.rgp.internalcommon.response.TerminalListResponse;
import com.rgp.internalcommon.response.TerminalResponse;
import com.rgp.internalcommon.utils.JointUrlUtils;
import com.rgp.servicemap.service.TerminalService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-19 18:19
 */
@Service
public class TerminalServiceImpl implements TerminalService {

    @Value("${amap.key}")
    private String secretKey;

    @Value("${amap.sid}")
    private String secretSid;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResponseResult<TerminalResponse> addTerminal(String name,Long id) {
        Map<String, String> map = new HashMap<>();
        map.put("key",secretKey);
        map.put("sid",secretSid);
        map.put("name",name);
        map.put("desc",id + "");

        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.CREATE_TERMINAL_URL, map);
        String restResponse = restTemplate.postForObject(url, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(restResponse);
        String errcode = jsonObject.getString("errcode");
        if (!errcode.equals("10000")){
            return ResponseResult.fail(CommonStatusEnum.CREATE_TERMINAL_ERROR);
        }
        JSONObject data = jsonObject.getJSONObject("data");
        Long tid = data.getLong("tid");
        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);
        return ResponseResult.success(terminalResponse);
    }

    @Override
    public ResponseResult deleteTerminal(String tid) {
        Map<String, String> map = new HashMap<>();
        map.put("key",secretKey);
        map.put("sid",secretSid);
        map.put("tid",tid);

        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.DELETE_TERMINAL_URL, map);
        String restResponse = restTemplate.postForObject(url, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(restResponse);
        String errcode = jsonObject.getString("errcode");
        if (!errcode.equals("10000")){
            return ResponseResult.fail(CommonStatusEnum.DELETE_TERMINAL_ERROR);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult listTerminal(String tid) {
        Map<String, String> map = new HashMap<>();
        map.put("key",secretKey);
        map.put("sid",secretSid);
        if (!StringUtils.isEmpty(tid)){
            map.put("tid",tid);
        }
        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.LIST_TERMINAL_URL, map);
        String restResponse = restTemplate.getForObject(url, String.class);
        JSONObject jsonObject = JSONObject.parseObject(restResponse);
        String errcode = jsonObject.getString("errcode");
        if (!errcode.equals("10000")){
            return ResponseResult.fail(CommonStatusEnum.LIST_TERMINAL_ERROR);
        }
        JSONObject data = jsonObject.getJSONObject("data");
        Integer count = data.getInteger("count");
        JSONArray results = data.getJSONArray("results");

        List<TerminalResponse> list = new ArrayList<>();
        JSONObject temp = null;
        for (int i = 0;i < results.size();i++){
            temp = results.getJSONObject(i);
            TerminalResponse terminalResponse = new TerminalResponse();
            terminalResponse.setTid(temp.getLong("tid"));
            terminalResponse.setName(temp.getString("name"));
            terminalResponse.setDesc(temp.getString("desc"));
            terminalResponse.setCreatetime(temp.getLong("createtime"));
            terminalResponse.setLocatetime(temp.getLong("locatetime"));
            list.add(terminalResponse);
        }
        TerminalResponse[] ts = list.toArray(new TerminalResponse[list.size()]);
        TerminalListResponse listResponse = new TerminalListResponse();
        listResponse.setCount(count);
        listResponse.setResults(ts);
        return ResponseResult.success(listResponse);
    }

    @Override
    public ResponseResult<AroundSearchResponse[]> aroundSearch(AroundSearchDto aroundSearchDto) {
        String center = aroundSearchDto.getCenter();
        Integer radius = aroundSearchDto.getRadius();
        Map<String, String> map = new HashMap<>();
        map.put("key",secretKey);
        map.put("sid",secretSid);
        map.put("center",center);
        map.put("radius",radius + "");

        String url = JointUrlUtils.combineUrlAndParameters(UrlConstants.AROUND_SEARCH_URL, map);
        String restResponse = restTemplate.postForObject(url,null,String.class);
        JSONObject jsonObject = JSONObject.parseObject(restResponse);
        String errcode = jsonObject.getString("errcode");
        if (!errcode.equals("10000")){
            return ResponseResult.fail(CommonStatusEnum.AROUND_SEARCH_FAIL_EXCEPTION);
        }
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray results = data.getJSONArray("results");
        JSONObject temp = null;
        ArrayList<AroundSearchResponse> list = new ArrayList<>();
        for (int i = 0;i < results.size();i++){
            temp = results.getJSONObject(i);
            Long tid = temp.getLong("tid");
            String carId = temp.getString("desc");
            JSONObject location = temp.getJSONObject("location");
            Double latitude = location.getDouble("latitude");
            Double longitude = location.getDouble("longitude");
            AroundSearchResponse response = new AroundSearchResponse();
            response.setTid(tid);
            response.setCarId(carId);
            response.setLatitude(latitude);
            response.setLongitude(longitude);
            list.add(response);
        }
        AroundSearchResponse[] array = list.toArray(new AroundSearchResponse[results.size()]);
        return ResponseResult.success(array);
    }
}